//
//  WebServisCaller.m
//  Mobil Izleme
//
//  Created by Mehmet Sinan Şahin on 1/2/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import "WebServiceCaller.h"
#import "NGDataRetrievingService.h"
#import "NGLocation.h"
#import "SetLocationRequest.h"
#import "SetLocationsRequest.h"
#import "SetLocationResponse.h"
#import "SendMessageRequest.h"
#import "LoginRequest.h"
#import "NSDate+Extensions.h"
#import "NGArrayOfDeviceData.h"
#import "SharedPrefs.h"


@implementation WebServisCaller

+(void)callWebService:(SetLocationRequest *)request {
    [self callWebService:request target:nil];
}


+(void)callWebService:(SetLocationRequest *)request 
                                target:(id)target {
    
	NGDataRetrievingService* service = [NGDataRetrievingService service];
	service.logging = NO;
    
    NSLog(@"service URL: %@\n", service.serviceUrl);
    
	//service.serviceUrl = @"http://123.1233.323"; //url hatası için test
    service.serviceUrl = request.serviceUrl;
    
    NGLocation *location = request.location;
    
    id setLocationTarget = target;
    
    if (setLocationTarget == nil)
        setLocationTarget = [[WebServisCaller alloc] autorelease];
    
    if ([request isKindOfClass:[SendMessageRequest class]]) {
        [service setLocation: setLocationTarget
                      action: @selector(sendMessageHandler:)
                  deviceCode: [NGUtility getDeviceId]
              lastLocation_X: location.lat 
              lastLocation_Y: location.lon 
                   tarihSaat: location.tarihSaat
                       speed: location.speed 
                      status: location.status
                 messageType: ((SendMessageRequest *)request).messageType
                 messageBody: ((SendMessageRequest *)request).messageBody
                  nameValues: [NSMutableArray array]
                   sessionID: [SharedPrefs getSessionID]];
        NSLog(@"setLocation(send message) çalıştırıldı.");
    } else {
        [service setLocation: setLocationTarget 
                      action: @selector(setLocationHandler:)
                  deviceCode: [NGUtility getDeviceId]
              lastLocation_X: location.lat 
              lastLocation_Y: location.lon 
                   tarihSaat: location.tarihSaat 
                       speed: location.speed 
                      status: location.status 
                  nameValues: [NSMutableArray array]];        
    }
    
	NSLog(@"setLocation çalıştırıldı.");	
    
}


+ (void)callWebServiceSetLocations:(SetLocationsRequest *)request target:(id)target {
    
	NGDataRetrievingService* service = [NGDataRetrievingService service];
	service.logging = YES;
	service.serviceUrl = request.serviceUrl;
 
    
    id setLocationTarget = target;
    
    if (setLocationTarget == nil)
        setLocationTarget = [[WebServisCaller alloc] autorelease];
    
    
    NGArrayOfDeviceData *arr = [[NGArrayOfDeviceData alloc] initWithArray:request.locationArray];
    
    [service setLocations:setLocationTarget action:@selector(setLocationsHandler:) dataPackage: (NSMutableArray *)arr];
    [arr release];
}

+ (void)callWebServiceLogin:(LoginRequest *)request target:(id)target {
    NGDataRetrievingService* service = [NGDataRetrievingService service];
    service.logging = TRUE;
    id loginTarget = target;
    
    if (loginTarget == nil)
        loginTarget = [[WebServisCaller alloc] autorelease];
    
    [service Login:loginTarget action:@selector(loginHandler:) userName:request.userName password:request.password];
}

#pragma mark - setLocation handler

- (void) setLocationHandler: (id) value {
  
	if([value isKindOfClass:[NSError class]]) {
		NSLog(@"NSERROR.....%@", value);
//        [testSetLocationResultText setText:[NSString stringWithFormat:@"NSERROR.....\n%@\n%@", dateStr, value]];        
//        [self hideActivityView];
		return;
	}
    
	// Handle faults
	if([value isKindOfClass:[SoapFault class]]) {
		NSLog(@"SOAP FAULTxx.....%@", value);
//        [testSetLocationResultText setText:[NSString stringWithFormat:@"SOAP FAULTxx.....\n%@\n%@", dateStr, value]];       
//        [self hideActivityView];
		return;
	}				
    
    
	// Do something with the NSString* result
    NSString* result = (NSString*)value;
	NSLog(@"setLocation returned the value: %@\n", result);
    
//    NSString *log = [NSString stringWithFormat:@"%@\n%@", dateStr, result];
//    [testSetLocationResultText setText:log];
//    [self hideActivityView];
}

#pragma mark - login handler

- (void) loginHandler: (id) value {
	if([value isKindOfClass:[NSError class]]) {
        
		NSLog(@"%@login handler NSERROR.....%@", __FUNCTION__, value);
		return;
	}

	if([value isKindOfClass:[SoapFault class]]) {
		NSLog(@"%@login handler SOAP FAULT.....%@", __FUNCTION__, value);
		return;
	}				

    NSString* result = (NSString*)value;
	NSLog(@"%@login returned the value: %@\n", __FUNCTION__, result);
    [SharedPrefs setSessionID:value];
}


@end
